//
//  BankMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/9.
//

import Foundation
import UIKit

class BankMsgVM: UIView {
    
    let selfHeight = kFitWidth(147)
    
    var updateBlock:(()->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(13), y: frame.origin.y, width: kFitWidth(347), height: selfHeight))
        self.backgroundColor = WHColorWithAlpha(colorStr: "FFB2B2", alpha: 0.12)
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(8)
        self.clipsToBounds = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var bankNameLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 20, weight: .medium)
        lab.adjustsFontSizeToFitWidth = true
//        lab.text = "中信银行"
        
        return lab
    }()
    lazy var updateButton : UIButton = {
        let btn = UIButton()
        btn.setTitle("更改", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 15, weight: .regular)
        
        btn.addTarget(self, action: #selector(updateAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var bankTypeLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        lab.text = "储蓄卡"
        
        return lab
    }()
    lazy var bankNoLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 20, weight: .bold)
        lab.adjustsFontSizeToFitWidth = true
//        lab.text = "**** **** **** 2398"
        
        return lab
    }()
    lazy var bankLogoImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "bank_img")
        
        return img
    }()
}

extension BankMsgVM{
    @objc func updateAction() {
        if self.updateBlock != nil{
            self.updateBlock!()
        }
    }
    func updateUI(dict:NSDictionary) {
        bankNameLabel.text = dict["bankName"]as? String ?? ""
        bankNoLabel.text = dict["bankCard"]as? String ?? ""
    }
    func updateUIForAgent(dict:NSDictionary) {
        bankNameLabel.text = dict["bankName"]as? String ?? ""
        bankNoLabel.text = dict["bankCardId"]as? String ?? ""
    }
}
extension BankMsgVM{
    func initUI() {
        addSubview(bankNameLabel)
        addSubview(updateButton)
        addSubview(bankTypeLabel)
        addSubview(bankLogoImgView)
        addSubview(bankNoLabel)
        
        setConstrait()
    }
    func setConstrait() {
        bankNameLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(18))
            make.top.equalTo(kFitWidth(19))
            make.width.equalTo(kFitWidth(230))
        }
        updateButton.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-5))
            make.centerY.lessThanOrEqualTo(bankNameLabel)
            make.width.equalTo(kFitWidth(50))
            make.height.equalTo(kFitWidth(22))
        }
        bankTypeLabel.snp.makeConstraints { make in
            make.left.equalTo(bankNameLabel)
            make.top.equalTo(kFitWidth(53))
        }
        bankNoLabel.snp.makeConstraints { make in
            make.left.equalTo(bankNameLabel)
            make.right.equalTo(kFitWidth(-120))
            make.bottom.equalTo(kFitWidth(-33))
        }
        bankLogoImgView.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
            make.width.equalTo(kFitWidth(88))
            make.height.equalTo(kFitWidth(66))
        }
    }
}
